package com.itheima.web.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * @author Tensin
 * @version 1.0
 * @date
 */

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{

    private BrandService brandService = new BrandServiceImpl();

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //调用Service查询寻
        List<Brand> brands = brandService.selectAll();


        //转成Json（使用fastjJson）
        String jsonString = JSON.toJSONString(brands);

        //写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    //添加数据
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        //先接收从前端页面传来的Json数据
        BufferedReader reader = request.getReader();
        String jsonParam = reader.readLine();//Json的字符串


        //转成Brand对象
        Brand brand = JSON.parseObject(jsonParam, Brand.class);

        //调用Service添加
        brandService.add(brand);

        //响应成功标识
        response.getWriter().write("success");
    }

    //单个删除
    public void deleteSingle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //先接收从前端页面传来的Json数据
        BufferedReader reader = request.getReader();
        String jsonParam = reader.readLine();//Json的字符串

        //转成int对象
        Integer deleteIdInt = JSON.parseObject(jsonParam, int.class);

        //调用service删除
        brandService.deleteSingle(deleteIdInt);

        //相应成功标识
        response.getWriter().write("deleteSuccess");

    }

    //批量删除
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //先接收从前端页面传来的Json数据
        BufferedReader reader = request.getReader();
        String jsonParam = reader.readLine();//Json的字符串

        //转成int对象
        int[] deleteIds = JSON.parseObject(jsonParam, int[].class);
        for (int i = 0; i < deleteIds.length; i++) {
            System.out.println(deleteIds[i]);
        }


        //调用service删除
        brandService.deleteByIds(deleteIds);

        System.out.println("批量删除成功");
        //相应成功标识
        response.getWriter().write("deleteIdsSuccess");
    }

    //分页查询
    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //前端用get的方从传输数据。
        String _currentPage = request.getParameter("currentPage"); //当前是第几页
        String _pageSize = request.getParameter("pageSize"); //每一页需要显示的条数

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);



        //调用Service查询寻
        PageBean<Brand> brandPageBean = brandService.selectByPage(currentPage, pageSize);
        String jsonPageBean = JSON.toJSONString(brandPageBean);

        //写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonPageBean);

    }

    //分页条件查询
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");

       //前端用get的方从传输数据。
        String _currentPage = request.getParameter("currentPage"); //当前是第几页
        String _pageSize = request.getParameter("pageSize"); //每一页需要显示的条数

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        //接收从前端页面传来的Json数据
        BufferedReader reader = request.getReader();
        String jsonParam = reader.readLine();//Json的字符串

        //转成Brand对象
        Brand brand = JSON.parseObject(jsonParam, Brand.class);


        //调用Service查询寻
        PageBean<Brand> brandPageBean = brandService.selectByPageAndCondition(currentPage, pageSize,brand);
        String jsonPageBean = JSON.toJSONString(brandPageBean);

        //写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonPageBean);

    }


}
